package cn.wit.luohui.day12;

import java.io.File;

import org.junit.Test;

public class DemoRegex {
//判断文件是否是excel文件
	@Test
	public void test() {
//		String fileName="‪C:\\Users\\luohui\\Desktop\\Java相关";
		String fileName="C:\\Users\\luohui\\Desktop\\Java相关";
		//System.out.println(new File(fileName).exists());
	
		listDir(new File(fileName));
	}

	public static void listDir(File dir) {
		File[] listFiles=dir.listFiles();
		
		for(File f:listFiles) {//遍历
			
			if(f.isDirectory()) {
				listDir(f);
			}else {
//				System.out.println(f);
				if(isExcel2(f.getName())) {
					//System.out.println(f.getName());
				}
			}
		}
		
	}
	//常规
	public static boolean isExcel(String fileName) {
		int c=fileName.lastIndexOf(".");//取到.的位置
		String suffix=fileName.substring(c+1);//后缀即为.的下一个位置
		
		if("xlsx".equals(suffix)) {
			return true;
		}else {
			return false;
		}
	}
	//正则表达式匹配文件后缀名
	//regex=> regular expression
	public static boolean isExcel2(String fileName) {
		//
		return fileName.matches(".*\\.(docx|xlsx)");
		
	}
	
	@Test
	public void testSplit() {
		String str="Let me make one thing clear . If you are a beginner programmer,\r\n" + 
				"this article is not meant to make you feel bad about the mistakes that\r\n" + 
				"you might be making but rather to make you aware of them, teach you\r\n" + 
				"to spot signs of them, and remind you to avoid them.";
		String[] strs=str.split("[,;.*\r\n]");
		for(String s:strs) {
			//System.out.println(s);
		}
	}
	@Test
	public void testReplace() {
		String str="luohui666中国aaa";
		String str2=str.replaceAll("[0-9]", "");//将数字替换掉
		//System.out.println(str2);
	}
	@Test
	public void test1() {
		String regex="^luo";//必须是luo
//		System.out.println("abc".matches(regex));
//		System.out.println("luo".matches(regex));
//		System.out.println("luo666".matches(regex));
//		System.out.println("lu6o".matches(regex));
	}
	@Test
	public void test2() {
		String regex2=".*abc$";//.*表任意字符，必须以abc结尾
//		System.out.println("abc".matches(regex2));
//		System.out.println("aaaabc".matches(regex2));
	}
	//表次数的
	//*0个以上包括0个
	//+1个以上包括1个
	//?0个或1个
	//{n}出现n次
	//{n,m}出现n到m次，包括n,m
	
	@Test
	public void test3() {
//		String regex3="abc*d";//abd,c出现0或以上则为true
//		System.out.println("abd".matches(regex3));
//		System.out.println("abc".matches(regex3));
		
//		String regex3="(abc)*d";//abd,abc出现0或以上,而d必须在abc后面则为true，即按顺序
//		System.out.println("abd".matches(regex3));
//		System.out.println("abc".matches(regex3));
//		System.out.println("abcabcddd".matches(regex3));
//		System.out.println("abcabcabcd".matches(regex3));
		
//			String regex3="abc{1,3}d";//按顺序，c出现1-3次即为true
	//		System.out.println("abd".matches(regex3));	
	//		System.out.println("abc".matches(regex3));
	//		System.out.println("abcdd".matches(regex3));
	//		System.out.println("abccd".matches(regex3));
	//		System.out.println("abccccd".matches(regex3));
		
//		String regex3="abc?d";//按顺序，c出现1-3次即为true
//		System.out.println("abd".matches(regex3));
//		System.out.println("abc".matches(regex3));
//		System.out.println("abcdd".matches(regex3));
//		System.out.println("abccd".matches(regex3));
//		System.out.println("abcd".matches(regex3));
		
		
	}
	@Test
	public void test4() {
		//.+\.(png|gif|jpn)$   .表任意字符+.+以(png|gif|jpn)里的任意一个结尾
		String regex4=".+\\.(png|gif|jpn|jpeg)$";
		String path="‪C:\\Users\\luohui\\Desktop\\1530764039764.jpeg";
		System.out.println(path.matches(regex4));
		
		String regex5="[abcd]";
		String regex6="(a|b|666|abd)";
//		System.out.println("a".matches(regex5));
//		System.out.println("abcd".matches(regex6));
		
		String regex7="[a-zA-Z0-9]";//A到Z,a-z,0-9的任意一个
//		System.out.println("a".matches(regex7));
//		System.out.println("A".matches(regex7));
//		System.out.println("0".matches(regex7));
//		System.out.println("9".matches(regex7));
//		System.out.println("中".matches(regex7));
//		System.out.println("-".matches(regex7));
		
		//System.out.println("aaaAAA889".matches(regex4));//false只能是一个
		
		//\d[0-9]
		//\w[a-zA-Z0-9]
//		System.out.println("99992".matches("\\d+"));
//		System.out.println("aaaAAA000999_".matches("\\w+"));//_可以
//		System.out.println("aaaAAA000999_-".matches("\\w+"));

	}
	@Test
	public void test5() {
		//密码必须是6-18位  \{6,18}
		String regex="\\w{6,18}";
		System.out.println("123456".matches(regex));
//		System.out.println("1234".matches(regex));
//		System.out.println("passw0rd".matches(regex));
//		System.out.println("ab$d".matches(regex));
//		System.out.println("abcdabcdabcdabcdabcd".matches(regex));
//		System.out.println("abcdefg".matches(regex));
		
		//密码必须是6-18位,必须含有数字
		String regex2="\\w{6,18}";
		
		//必须含有数字
		String regex3=".*\\d+.*";
		//System.out.println("a3c".matches(regex3));
		
		//断言:必须是      (?=.*\d+)\w{6,18}
		String regex4="(?=.*\\d+.*).{6,18}";
		System.out.println("abcd1234".matches(regex4));
//		System.out.println("abcdefgh".matches(regex4));
//		System.out.println("abcdefg3".matches(regex4));
		
		//密码是6-18位，必须含有数字，必须是大写字母开头
		//^[A-Z]
		//(?=)(?=)
		//String regex5="(?=.*\\d+)(?=^[A-Z])\\w{6,18}";
		
		// ^[A-Z]\w{5,17}
				String regex5 = "(?=.*\\d+)^[A-Z]\\w{5,17}";
				System.out.println("Abcd1234".matches(regex5));
	}
	@Test
	public void test6() {
		//非负整数：0+正整数
		
		//[1-9][0-9]|[0-9]
//		String a="[1-9][0-9]|[0-9]";
//		String reg1 = "[1-9][0-9]*|0";
//		String reg2 = "[0-9]|[1-9][0-9]+";
//		
//		System.out.println("1".matches(a));
//		System.out.println("0123".matches(reg1));
//		System.out.println("123".matches(reg2));
//		System.out.println("0123".matches(reg2));
		
		String reg1 = "[\\+\\-]?[1-9]\\d*|0";//[\\+\\-]?0个或者1个
		System.out.println("+123".matches(reg1));
		System.out.println("123".matches(reg1));
		System.out.println("+-123".matches(reg1));
		
	}
	@Test
	public void test7() {
		// 20-99的数字
//		String regex = "[20-99]";	// [0-9]
//		String regex = "(20-99)";	// [0-9]
		String regex = "[2-9][0-9]";	// [0-9]
		System.out.println("20".matches(regex));
		System.out.println("99".matches(regex));
		System.out.println("9".matches(regex));
		System.out.println("100".matches(regex));
		
		// 20-199  20-99 100-199
	}
	@Test
	public void test8() {
		//·正则表达式手机号
		//((13[0-9])|(14[5|7])|(15([-3]|[5-9]))|(18[0,5-9]))\d{8}
		String regex = "((13[0-9])|(14[5|7])|(15([-3]|[5-9]))|(18[0,5-9]))\\d{8}";
	System.out.println("18562328957".matches(regex));
	}
	@Test
	public void testURL() {
		//判断是不是网址
		// /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
				String s = "^((ht|f)tps?):\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-"
						+ "\\.,@?^=%&:\\/~\\+#]*[\\w\\-\\@?^=%&\\/~\\+#])?$";
				
				System.out.println("http://baidu.com".matches(s));
				System.out.println("http://baidu".matches(s));
				System.out.println("mailto://baidu.com".matches(s));

	}
	}

